1 module targets.wasm;
2 import features.git;
3 import commons;
4 import global_opts;
5 import server;
6 import tools.gendir;
7 import tools.releasegame;
8 
9 
10 ChoiceResult prepareWASM(Choice* c, ref Terminal t, ref RealTimeConsoleInput input, in CompilationOptions cOpts)
11 {
12 	import std.conv:to;
13 	if(!hasLdc)
14 	{
15 		t.writelnError("WASM build requires ldc2 in path. Please install it before building to it.");
16 		return ChoiceResult.Error;
17 	}
18 
19 	inParallel(
20 		cached(() => timed(t, submoduleLoader.execute(t, input))),
21 		putResourcesIn(t, getHipPath("build", "wasm", "build", "assets")),
22 		generateDirectoriesJSON(
23 			buildNormalizedPath(configs["gamePath"].str, "assets"),
24 			getHipPath("build", "wasm", "generated")
25 		),
26 		//The template may not be present
27 		outputTemplate(t, configs["gamePath"].str),
28 	);
29 
30 
31 	environment["DFLAGS"] = 
32 		"-I="~getHipPath("modules", "d_std", "source") ~" "~
33 		"-I="~getHipPath("dependencies", "runtime", "druntime", "arsd-webassembly") ~" " ~
34 		"-I="~getHipPath("dependencies", "runtime", "druntime", "source") ~" " ~
35 		"-float-abi=hard "~
36 		"--fvisibility=hidden "~
37 		"-mattr=+bulk-memory "~
38 		// "-real-precision=double "~
39 		"-L-allow-undefined -d-version=CarelessAlocation";
40 
41 
42 	with(WorkingDir(configs["gamePath"].str))
43 	{
44 		ProjectDetails project;
45 		if(waitRedub(t, DubArguments()
46 			.command("build").compiler("ldc2").build("debug").configuration("release-wasm")
47 			.arch("wasm32-unknown-unknown-wasm").opts(cOpts), project ) != 0)
48 		{
49 			t.writelnError("Could not build for WebAssembly.");
50 			return ChoiceResult.Error;
51 		}
52 		// import wasm_sourcemaps.generate;
53 
54 		///In the current status, wasm sourcemap generation invalidates cache, but since the compilation is really fast right now
55 		///We can keep that
56 		// string[] out_Errors;
57 		// if(!timed(t, "Generating WASM Sourcemaps ", generateSourceMaps(null, getHipPath("hipreme_engine.wasm"), "D:", shouldEmbed: false, includeSources:true, out_Errors)))
58 		// 	t.writelnError(out_Errors);
59 		// if(out_Errors.length) foreach(err; out_Errors)
60 		// 	t.writelnError(err);
61 
62 		string file = project.getOutputFile();
63 		if(std.file.exists(file))
64 		{
65 			t.writelnHighlighted("Renaming ", file, " to hipreme_engine.wasm for compatibility");
66 			std.file.rename(file, getHipPath("build", "wasm", "build", "hipreme_engine.wasm"));
67 		}
68 	}
69 	if(!serverStarted)
70 	{
71 		t.writelnHighlighted("Attempt to start WebAssembly development server.");
72 		startServer(&gameServerPort, &gameServerHost, t);
73 		t.writelnSuccess("Development started at localhost:"~gameServerPort.to!string);
74 		cached(() => cast(void)openDefaultBrowser("http://"~gameServerHost~":"~gameServerPort.to!string));
75 	}
76 	else
77 	{
78 		t.writelnSuccess("Succesfully built for WebAssembly. Listening on http://"~gameServerHost~":"~gameServerPort.to!string);
79 		pushWebsocketMessage("reload");
80 	}
81 
82 
83 
84 	return ChoiceResult.None;
85 }